<template>
  <div class="mod-config">
    <basic-container>
      <navigationRenWuShu
        :sign="{ a: true, b: false, c: false, d: false, e: false, f: false }"
      ></navigationRenWuShu>
      <div class="boxsty zhiBiao">
        <div :height="searchheight">
          <span
            style="float: right; margin-top: -30px; cursor: pointer"
            @click="searchchangeadvanced"
            >{{ searchadvanced ? "查询条件收起" : "查询条件展开" }}
            <i
              :class="
                searchadvanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'
              "
            />
          </span>
          <div v-show="searchadvanced">
            <el-form
              :inline="true"
              :model="dataForm"
              @keyup.enter.native="getDataList()"
              class="form-inline"
            >
              <el-form-item label="任务书名称">
                <el-input
                  v-model="dataForm.workAssignName"
                  placeholder="任务书名称"
                  :clearable="true"
                  style="width: 150px"
                ></el-input>
              </el-form-item>
              <el-form-item label="被考核人姓名">
                <el-input
                  v-model="dataForm.ryName"
                  placeholder="被考核人姓名"
                  :clearable="true"
                  style="width: 150px"
                ></el-input>
              </el-form-item>
              <el-form-item label="被考核人岗位">
                <el-input
                  style="width: 150px"
                  v-model="dataForm.userZw"
                  placeholder="被考核人岗位"
                  :clearable="true"
                ></el-input>
              </el-form-item>
              <el-form-item label="被考核人职级">
                <el-input
                  style="width: 150px"
                  v-model="dataForm.userZj"
                  placeholder="被考核人职级"
                  :clearable="true"
                ></el-input>
              </el-form-item>
              <el-form-item label="任务状态">
                <el-select
                  style="width: 150px"
                  v-model="dataForm.status"
                  :clearable="true"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in optionsStatus"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button
                  icon="el-icon-search"
                  type="primary"
                  class="marginLeft"
                  @click="getDataList()"
                  >检索
                </el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
        <div
          style="
            display: flex;
            justify-content: flex-end;
            border-top: 1px solid #b0bde2;
            padding-top: 10px;
          "
        >
          <div>
            <el-button
              type="primary"
              plain
              @click="endBtn"
              v-loading.fullscreen.lock="fullscreenLoadingend"
              >结束</el-button
            >
            <el-button type="primary" plain @click="temBtn">模板管理</el-button>
            <el-button
              type="primary"
              plain
              @click="submitBtn"
              v-loading.fullscreen.lock="fullscreenLoadingsubmit"
              >提交</el-button
            >
          </div>
        </div>
        <el-table
          ref="tableRef"
          style="width: 100%; margin-top: 20px"
          :data="dataList"
          border
          v-loading="dataListLoading"
        >
          <el-table-column
            type="selection"
            width="55"
            header-align="center"
            align="center"
            :selectable="checkboxSelect"
          ></el-table-column>
          <el-table-column
            type="index"
            width="80"
            header-align="center"
            align="center"
            label="序号"
          ></el-table-column>
          <el-table-column
            prop="workAssignName"
            header-align="center"
            align="center"
            width="160"
            show-overflow-tooltip
            label="任务书名称"
          >
            <template slot-scope="scope">
              <span
                @click="editBtn(scope.row)"
                style="color: #0e258c; cursor: pointer"
                >{{ scope.row.workAssignName }}</span
              >
            </template>
          </el-table-column>
          <el-table-column
            prop="userName"
            header-align="center"
            align="center"
            min-width="150"
            label="被考核人编号"
          >
          </el-table-column>
          <el-table-column
            prop="ryName"
            header-align="center"
            align="center"
            min-width="150"
            label="被考核人姓名"
          >
          </el-table-column>
          <el-table-column
            prop="userDeptName"
            header-align="center"
            align="center"
            min-width="150"
            label="被考核人单位"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="userZw"
            header-align="center"
            align="center"
            min-width="170"
            label="被考核人岗位"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="userZj"
            header-align="center"
            align="center"
            min-width="170"
            label="被考核人职级"
          >
          </el-table-column>
          <el-table-column
            prop="zhibiaoNum"
            header-align="center"
            align="center"
            min-width="170"
            label="指标数量"
          >
          </el-table-column>
          <el-table-column
            prop="status"
            header-align="center"
            align="center"
            width="140"
            label="状态"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.status == 0">未提交</span>
              <span v-else>已提交</span>
            </template>
          </el-table-column>
        </el-table>
        <div class="avue-crud__pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            :total="totalPage"
            background
            layout="total, sizes, prev, pager, next, jumper"
          >
          </el-pagination>
        </div>
      </div>
      <temManagement
        :temanagementDialogData="temanagementDialogData"
        @temManagementChildren="temManagementChildren"
      ></temManagement>
      <editDialog ref="editDialog" @editChildren="getDataList"></editDialog>
    </basic-container>
    <UserCommonSelect ref="usercomment" @submitfy="submitfy"></UserCommonSelect>
    <el-dialog title="请选择：" :visible.sync="chooseVisible" width="30%">
      <div style="display: flex; justify-content: space-around">
        <el-button type="warning" @click="customBtn">自定义流程</el-button>
        <el-dropdown>
          <el-button type="primary" size="small" v-if="temList.length > 0">
            模板<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-button type="primary" size="small" v-else> 暂无模板 </el-button>
          <el-dropdown-menu v-if="temList.length > 0">
            <el-dropdown-item v-for="(item, index) in temList" :key="index"
              ><span
                @click="temsubmitBtn(item)"
                v-loading.fullscreen.lock="fullscreenLoadingmb"
                >{{ item.templateName }}</span
              ></el-dropdown-item
            >
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import editDialog from "./editDialog";
import UserCommonSelect from "@/views/hetongrenwu/components/usercomment-select";
import { getListMb } from "@/api/hetongrenwu/hetongjgtb";
import {
  renwuNdList,
  confirmMubiao,
  submitStatusAll,
  ndendTask,
} from "@/api/hetongrenwu/book/bookNd";
import temManagement from "./temanagementDialog.vue";
import { getRenWuFaQi } from "@/api/hetongrenwu/zhongdianrenwu/zhongdianrwcj";
export default {
  components: {
    temManagement,
    UserCommonSelect,
    editDialog,
  },
  data() {
    return {
      optionsStatus: [
        {
          value: 0,
          label: "未提交",
        },
        {
          value: 1,
          label: "已提交",
        }
      ],

      dataForm: {},
      dataList: [],
      dataListLoading: false,
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      searchadvanced: true,
      searchheight: "0",
      temanagementDialogData: {},
      fullscreenLoadingend: false,
      shouyuerendataList: [],
      chooseVisible: false,
      fullscreenLoadingmb: false,
      temList: [],
      jsonData: "",
      addDataForm: {},
      addDialogData: {},
      isend: Number,
      fullscreenLoadingsubmit: false,
    };
  },
  created() {
    this.addDataForm = {};
    this.getDataList();
    this.fngetListMb();
  },
  watch: {},
  methods: {
    searchchangeadvanced() {
      this.searchadvanced = !this.searchadvanced;
      if (this.searchadvanced) {
        this.searchheight = "auto";
      }
    },
    //查看
    checkDetails(row) {},
    // 新增
    addBtn() {
      getRenWuFaQi().then((res) => {
        this.addDataForm = res.data.data;
        if (this.addDataForm === null) {
          this.$message.error("当前没有启动中的任务！");
          return false;
        }
        this.addDialogData = {
          visible: true,
        };
      });
    },
    editBtn(row) {
      this.$refs.editDialog.init(row);
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      renwuNdList(
        Object.assign({
          current: this.pageIndex,
          size: this.pageSize,
        },this.dataForm)
      ).then((response) => {
        this.dataList = response.data.data.records;
        this.totalPage = response.data.data.total;
        this.dataListLoading = false;
      });
    },
    checkboxSelect(row, rowIndex) {
      if (row.status == 0) {
        return true;
      } else {
        return false;
      }
    },
    fngetListMb() {
      var params = {
        menuType: "工作任务书拟定",
        tagName: "工作任务书",
      };
      getListMb(params).then((res) => {
        this.temList = res.data.data;
      });
    },
    // 结束
    endBtn() {
      var rows = this.$refs.tableRef.selection;
      if (rows.length == 0) {
        this.$message.closeAll();
        this.$message({
          type: "info",
          message: "请至少选择一条数据结束!",
        });
        return false;
      }
      for (var i = 0; i < rows.length; i++) {
        if (rows[i].zhibiaoNum != 0) {
          this.$message.warning("请勾选任务数量为0的数据");
          return;
        }
      }
      var params = {
        rows: rows,
      };
      this.fullscreenLoadingend = true;
      ndendTask(params)
        .then((res) => {
          this.$message.closeAll();
          this.$message.success("结束成功");
          this.getDataList();
          this.fullscreenLoadingend = false;
        })
        .catch(() => {
          this.$message.closeAll();
          this.$message({
            type: "error",
            message: "提交失败!",
          });
          this.fullscreenLoadingend = false;
        });
    },
    // 模板管理
    temBtn() {
      this.temanagementDialogData = {
        dialogVisible: true,
        type: "工作任务书拟定",
      };
    },
    temsubmitBtn(e) {
      var params = {
        rows: this.$refs.tableRef.selection,
        type: 1,
        isend: 0,
        templateId: e.id,
      };
      this.fullscreenLoadingmb = true;
      submitStatusAll(params)
        .then((res) => {
          this.chooseVisible = false;
          this.getDataList();
          this.fullscreenLoadingmb = false;
          this.$message.success("提交成功");
        })
        .catch((res) => {
          this.chooseVisible = false;
          this.fullscreenLoadingmb = false;
          this.$message.warning("提交失败");
        });
    },
    customBtn() {
      this.$nextTick(() => {
        this.$refs.usercomment.init("12", "allTree", this.jsonData);
      });
    },
    temManagementChildren(data) {
      this.getDataList();
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    submitBtn() {
      var data = this.$refs.tableRef.selection;
      if (data.length == 0) {
        this.$message.warning("请勾选可提交的数据");
      }
      for (var i = 0; i < data.length; i++) {
        if (data[i].status == 1) {
          this.$message.warning("请选择未提交的数据");
          return;
        }
        if (data[i].zhibiaoNum == 0) {
          this.$message.warning("当前勾选的数据存在没有指标,不能提交！");
          return;
        }
      }
      confirmMubiao({ rows: data }).then((response) => {
        if (response.data.data) {
          this.$confirm("是否跳过指标审核", "提示", {
            confirmButtonText: "是",
            cancelButtonText: "否",
            type: "warning",
          })
            .then((res) => {
              this.fullscreenLoadingsubmit = true;
              this.isend = 1;
              var params = {
                isend: 1,
                rows: data,
              };
              submitStatusAll(params)
                .then((res) => {
                  this.$message.closeAll();
                  this.$message.success("提交成功");
                  this.getDataList();
                  this.fullscreenLoadingsubmit = false;
                })
                .catch(() => {
                  this.$message.closeAll();
                  this.$message({
                    type: "error",
                    message: "提交失败!",
                  });
                  this.fullscreenLoadingsubmit = false;
                });
            })
            .catch(() => {
              this.isend = 1;
              this.chooseVisible = true;
              this.fngetListMb();
            });
        } else {
          this.$message({
            type: "error",
            message: "目标值为空，请完善目标值!",
          });
        }
      });
    },
    submitfy(data) {
      var ids = "";
      data.tableDataUser.forEach((ele) => {
        ids = ids + ele.userId + ",";
      });
      this.fullscreenLoadingsubmit = true;
      submitStatusAll({
        rows: this.$refs.tableRef.selection,
        sonhq: ids,
        type: 2,
        isend: 0,
      })
        .then((res) => {
          this.$message.closeAll();
          this.$message({
            type: "info",
            message: "提交成功",
          });
          this.fullscreenLoadingsubmit = false;
          this.chooseVisible = false;
          this.getDataList();
        })
        .catch(() => {
          this.$message({
            type: "error",
            message: "提交失败!",
          });
          this.chooseVisible = false;
          this.fullscreenLoadingsubmit = false;
        });
    },
  },
};
</script>

<style>
</style>
